<?php

class partner 
{
    function partner()
    {
    }
    
    function validatePartner()
    {
    	$msg = "";
    	if(trim($_POST['partner_name']) == "")
    	{
    		$msg = _("Partner name can not be empty");
    	}
    	if(trim($_POST['partner_url']) == "")
    	{
    		if($msg != "") $msg .= "<br />";
    		$msg .= _("Partner URL can not be empty");
    	}

    	$urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*".
					"(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
    	if(trim($_POST['partner_url']) != "" && !eregi($urlregex, $_POST['partner_url']))
    	{
    		if($msg != "") $msg .= "<br />";
    		$msg .= _("Invalid URL");
    	}
    	
    	return $msg;
    }
    
    function deletePartner($db, $id)
    {
    	//Delete Banner
    	$sql = sprintf("DELETE FROM %1\$s" .
				" where %1\$s.partner_id = '$id'", 
				TBL_PARTNER);
		
    	$res = $db->query($sql);
  	
    	return $res;
    }
    
    function savePartner($db, $partner)
    {
    	$res = $db->query(sprintf(
					"INSERT INTO %s(partner_name, partner_image, partner_url, partner_status) " .
					"VALUES('%s', '%s', '%s', '%s')",
    				TBL_PARTNER, 
    				mysql_real_escape_string($partner['partner_name']),
    				$partner['partner_image'],
    				$partner['partner_url'],
    				$partner['partner_status']));
    	
    	if($res == true)
    		return $db->insert_id;
    	else
    		return -1;
    }
    
    function updatePartner($db, $partner)
    {
    	$res = $db->query(sprintf(
					"UPDATE %s SET " .
					"partner_name	= '".mysql_real_escape_string($partner['partner_name'])."', " .
					"partner_image 	= '$partner[partner_image]', " .
					"partner_url 	= '$partner[partner_url]', " .
					"partner_status	= '$partner[partner_status]' " .
					"WHERE partner_id = '$partner[partner_id]'",
    				TBL_PARTNER));
    	
    	return $res;
    }
    
    function getPartnerById($db, $id)
    {
    	$sql = sprintf("SELECT * FROM %s WHERE partner_id='$id'", TBL_PARTNER);
    	return $db->get_row($sql);
    }
    
    /*
     * Get all data from advertising table
     * each item is an object same as database structure
     * only get data that status = active
     */
    function getAllActivePartner($db)
    {
    	$sql = sprintf("SELECT * FROM %s WHERE partner_status='active'", TBL_PARTNER);
    	return $db->get_results($sql);
    }
    
     /*
     * Get all data from advertising table
     * each item is an object same as database structure
     */
    function getAllPartner($db)
    {
    	$sql = sprintf("SELECT * FROM %s", TBL_PARTNER);
    	return $db->get_results($sql);
    }
    
    function getAllActivePartnerSharePage($db)
    {
    	$page = isset ( $_GET["list"] ) ? intval ( $_GET["list"] ) : 1;
		$rows_per_page= MAX_DEMO_PARTNER;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
    	
    	$strSQL = sprintf("SELECT * FROM %s WHERE partner_status='active' LIMIT $page_start, $rows_per_page", TBL_PARTNER);
    	return $db->get_results($strSQL);
    }
    
    function countAllActivePartner($db)
    {
    	$strSQL = sprintf("SELECT COUNT(*) as count_partners FROM %s WHERE partner_status='active'", TBL_PARTNER);
    	$res = $db->get_row($strSQL);
    	return $res->count_partners;
    }
	

}
?>